import { acceptHMRUpdate, defineStore } from 'pinia'
import storage from '@/utils/storage'
import logoImage from '/src/assets/logo.jpg'

export const useUserStore = defineStore('user', () => {
  /**
   * Current name of the user.
   */
  const savedName = ref('')
  const previousNames = ref(new Set<string>())

  const usedNames = computed(() => Array.from(previousNames.value))
  const otherNames = computed(() => usedNames.value.filter((name) => name !== savedName.value))

  const logoImg = computed(() => storage.getLogoImg() || logoImage)
  const category = computed(() => JSON.parse(JSON.stringify(storage.getCategory())))

  /**
   * Changes the current name of the user and saves the one that was used
   * before.
   *
   * @param name - new name to set
   */
  function setNewName(name: string) {
    if (savedName.value) previousNames.value.add(savedName.value)

    savedName.value = name
  }

  return {
    setNewName,
    otherNames,
    savedName,
    logoImg,
    category
  }
})

if (import.meta.hot) import.meta.hot.accept(acceptHMRUpdate(useUserStore as any, import.meta.hot))
